package cn.qiyu5522.tk_ems.controller.admin;


import cn.hutool.core.collection.CollectionUtil;
import cn.qiyu5522.tk_ems.entity.Menu;
import cn.qiyu5522.tk_ems.entity.RoleMenu;
import cn.qiyu5522.tk_ems.entity.dto.RoleMenuEditDTO;
import cn.qiyu5522.tk_ems.entity.vo.RoleMenuVO;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.MenuService;
import cn.qiyu5522.tk_ems.service.RoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色-菜单关联表 前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-27
 */
@Api(tags = "角色菜单管理")
@RestController
@RequestMapping("admin/roleMenu")
public class RoleMenuController {

    @Resource
    private RoleMenuService roleMenuService;
    @Resource
    private MenuService menuService;
    @Resource
    private ObjectMapper objectMapper;

    @ApiOperation("查询角色菜单根据角色ID")
    @ApiImplicitParam(name = "roleId", value = "角色编号", dataType = "Integer")
    @GetMapping("/getRoleMenuListByRoleId/{roleId}")
    private R getRoleMenuListByRoleId(@PathVariable Integer roleId) {
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoleMenu::getRoleId, roleId);
        List<RoleMenu> list = roleMenuService.list(lambdaQueryWrapper);
        List<Integer> menuIds = list.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        List<Menu> menusList = null;
        if (CollectionUtil.isNotEmpty(menuIds)) {
            LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            menuLambdaQueryWrapper.in(Menu::getId, menuIds);
            menusList = menuService.list(menuLambdaQueryWrapper);
        }
        // 实体转VO
        List<RoleMenuVO> roleMenuVOS = objectMapper.convertValue(list, new TypeReference<List<RoleMenuVO>>() {
        });

        if (CollectionUtil.isNotEmpty(menusList)) {
            for (Menu menu : menusList) {
                roleMenuVOS.forEach(roleMenuVO -> {
                    if (roleMenuVO.getMenuId().equals(menu.getId())) {
                        roleMenuVO.setMenuName(menu.getTitle());
                    }
                });
            }
        }
        return R.ok().data("roleMenuListByRoleId", roleMenuVOS);
    }

    @ApiOperation("查询角色菜单根据角色ID")
    @ApiImplicitParam(name = "roleMenuEditDTO", value = "角色菜单编辑DTO", dataTypeClass = RoleMenuEditDTO.class)
    @PutMapping("/editByRowId")
    private R editByRowId(
            @Validated
            @RequestBody RoleMenuEditDTO roleMenuEditDTO
    ) {
        return roleMenuService.editByRowId(roleMenuEditDTO.getRoleId(), roleMenuEditDTO.getMenuIds());
    }


}
